package com.lh.controller;

import com.lh.pojo.bo.ShopCartBO;
import com.lh.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-10 22:05
 **/
@RestController
@RequestMapping("/shopcart")
@Api(value = "购物车接口controller", tags = {"购物车相关的api接口"})
public class ShopCartController {

    @PostMapping("/add")
    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public IMOOCJSONResult add(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId,
            @RequestBody ShopCartBO shopCartBO,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        if (StringUtils.isEmpty(userId)){
            return IMOOCJSONResult.errorMsg("用户Id为空!");
        }

        //TODO 在前端登录的情况下 后端会将购物车同步到redis中

        return IMOOCJSONResult.ok();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除商品到购物车", notes = "删除商品到购物车", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public IMOOCJSONResult del(
            @ApiParam(name = "用户Id", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "商品规格Id", value = "商品规格Id", required = true)
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(itemSpecId)){
            return IMOOCJSONResult.errorMsg("用户Id或商品规格Id为空!");
        }

        // TODO 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端购物车中的商品

        return IMOOCJSONResult.ok();
    }

}
